package com.xf.common.core.domain;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xf.common.core.enums.StatusCodeEnum;

import java.io.Serializable;

/**
 * 普通返回
 *
 * @author Administrator
 */


public class DmpResult<T> implements Serializable {
    private static final long serialVersionUID = 771836922595187944L;

    //返回数据

    protected T data;
    //返回错误码

    protected String code;
    //返回消息

    protected String msg;

    public DmpResult() {
    }

    public DmpResult(T t, String code, String msg) {
        this.data = t;
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功返回的默认方法
     *
     * @param
     * @return
     */
    public static DmpResult success() {
        return new DmpResult(null, StatusCodeEnum.SUC.getCode(), "result_success");
    }

    public static DmpResult success(String msg) {
        return new DmpResult(null, StatusCodeEnum.SUC.getCode(), msg);
    }

    /**
     * 数据返回的默认方法
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> DmpResult data(T t) {
        return new DmpResult(t, StatusCodeEnum.SUC.getCode(), "result_success");
    }

    public static <T> DmpResult data(T t, String msg) {
        return new DmpResult(t, StatusCodeEnum.SUC.getCode(), msg);
    }

    public static DmpResult data(String retCode, String msg) {
        return new DmpResult(null, retCode, msg);
    }

    /**
     * 错误的默认方法
     *
     * @param
     * @return
     */
    public static DmpResult failed() {
        return new DmpResult(null, StatusCodeEnum.ERROR.getCode(), "result_fail");
    }

    public static DmpResult failed(String msg) {
        return new DmpResult(null, StatusCodeEnum.ERROR.getCode(), msg);
    }

    public static DmpResult failed(StatusCodeEnum statusCodeEnum) {
        return new DmpResult(null, statusCodeEnum.getCode(), "result_fail");
    }

    public static DmpResult failed(StatusCodeEnum statusCodeEnum, String msg) {
        return new DmpResult(null, statusCodeEnum.getCode(), msg);
    }

    public static DmpResult status(boolean status) {
        return status ? success() : failed();
    }

    @JsonIgnore
    public Boolean isSuccess() {
        return StatusCodeEnum.SUC.getCode().equals(this.code);
    }

    @JsonIgnore
    public Boolean isRetData() {
        return isSuccess() && this.getData() != null;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
